package com.file;

import com.model.Book;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;

/**
 * @Description: 根据URL得到图书资源
 */
public class GetContent {
	public static void main(String[] args) {
		String url = "https://www.23txt.com/search.php?keyword=%E7%9B%97%E5%A2%93%E7%AC%94%E8%AE%B0";
		String encoding ="utf-8";
		String html = getHTMLResourceByUrl(url,encoding);
		System.out.println(html);//输出html

		ArrayList<Book> books = getBooksFromSearchHtml(html);
		System.out.println("---搜索的第一本书---");
		System.out.println("书名："+books.get(0).getName());
		System.out.println("作者名："+books.get(0).getAuthor());
		System.out.println("书籍种类："+books.get(0).getType());
		System.out.println("最新章节名："+books.get(0).getNewestChapterTitle());
		System.out.println("章节地址："+books.get(0).getChapterUrl());

	}


	public static ArrayList<Book> getBooksFromSearchHtml(String html) {
		ArrayList<Book> books = new ArrayList<>();
		Document doc = Jsoup.parse(html);
		Elements divs = doc.getElementsByClass("result-list");
		Element div = divs.get(0);
		for (Element element : div.children()) {
			Book book = new Book();
			Element img = element.child(0).child(0).child(0);
			book.setImgUrl(img.attr("src"));
			Element title = element.child(1).child(0);

//			Element title = element.getElementsByClass("result-item-title result-game-item-title").get(0);
			book.setName(title.child(0).attr("title"));
			book.setChapterUrl(title.child(0).attr("href"));
			Element desc = element.getElementsByClass("result-game-item-desc").get(0);
			book.setDesc(desc.text());
			Element info = element.getElementsByClass("result-game-item-info").get(0);
			for (Element element1 : info.children()) {
				String infoStr = element1.text();
				if (infoStr.contains("作者：")) {
					book.setAuthor(infoStr.replace("作者：", "").replace(" ", ""));
				} else if (infoStr.contains("类型：")) {
					book.setType(infoStr.replace("类型：", "").replace(" ", ""));
				} else if (infoStr.contains("更新时间：")) {
					book.setUpdateDate(infoStr.replace("更新时间：", "").replace(" ", ""));
				} else {
					Element newChapter = element1.child(1);
					book.setNewestChapterUrl(newChapter.attr("href"));
					book.setNewestChapterTitle(newChapter.text());
				}
			}
			books.add(book);

		}

		return books;
	}
	//获取html
	public static String getHTMLResourceByUrl(String url,String encoding){
		//使用File类创建一个要操作的文件路径
		String savePath = "F:/test/save.txt";
		File file = new File(savePath);
		//如果文件目录不出在则创建目录
		if(!file.getParentFile().exists()){//判断文件目录是否存在
			file.getParentFile().mkdirs();//创建目录
		}
		//可以创建url实例
		URL urlObj;

		StringBuffer sb = new StringBuffer();

		//字节输入流
		InputStream is = null;
		//字节流转字符流
		InputStreamReader isr = null;
		//转缓冲流  提高读取效率
		BufferedReader br = null;

		//文件输出流
		OutputStream output = null;
		//字符缓冲流输出  转化流
		BufferedWriter bw = null;

		try {
			urlObj = new URL(url);
			URLConnection openConnection =null;
			openConnection = urlObj.openConnection();
			sb = new StringBuffer();
			//字节输入流
			is = urlObj.openStream();
			//字节流转字符流
			isr = new InputStreamReader(openConnection.getInputStream(),encoding);
			//转缓冲流  提高读取效率
			br = new BufferedReader(isr);

			//文件输出流
			output = new FileOutputStream(file);
			//字符缓冲流输出  转化流
			bw = new BufferedWriter(new OutputStreamWriter(output));

			String line = null;
			while ((line = br.readLine()) != null) {
				System.out.println(line);
				sb.append(line + "\n");
			}
			bw.write(sb.toString());

		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally {
			try {
				bw.flush();
				bw.close();
				br.close();
				is.close();
				output.close();
				isr.close();

			} catch (IOException e) {
				e.printStackTrace();
			}


		}

		return sb.toString();
	}
	
}
